package kaka.utils.effect
{
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.system.System;
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.core.Container;
	import mx.core.IVisualElement;
	import mx.core.IVisualElementContainer;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.graphics.ImageSnapshot;
	
	import org.efflex.mx.effectClasses.BitmapInstance;
	
	import spark.components.BorderContainer;
	import spark.components.Group;
	import spark.components.SkinnableContainer;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.primitives.BitmapImage;

	public class EffectUtils
	{ 
		/**
		 * 
		 **/
		public static var awiseffect:String="awisEffect";
		public static var eachitemeffect:String="eachItemEffect";
		/**每行Item上下间距**/
		public static var itemVerticalSpace:Number=50;
		public static var lazyTime:Number=0.1;
		/**效果呈现方向**/
		public static const UP:String="up";
		public static const DWON:String="dwon";
		public static const LEFT:String="left";
		public static const RIGHT:String="right";
		
		public function EffectUtils()
		{
		}
		/**
		 * 仙女散花式效果
		 * @param container 仙女
		 * @param childList 花
		 **/
		public static function awisEffect(childList:ArrayCollection,container:UIComponent):void
		{
			/**每个Item间距**/
			var itemDistance:Number=(container.width)/(childList.length+1);
			trace(childList.length);
			trace(container.parent.width);
			trace(itemDistance);                                                                                                                               
			for(var i:int=0;i<childList.length;i++)
			{
				var itemX:Number=(itemDistance*(i+1))-childList[i].width/2;
				var itemY:Number=container.y+container.height+itemVerticalSpace;
				var fromX:Number=container.width/2;
				var fromY:Number=-container.height/2;
				TweenLite.from(childList[i],0.5,{x:fromX,y:fromY,alpha:0.0});
				TweenLite.to(childList[i],1.0,{x:itemX,y:itemY,delay:i*lazyTime,alpha:1.0,ease:Back.easeOut});
			}
		}
		/**
		 * 展现方块效果
		 * @param target       需要展现效果的目标
		 * @param showOrHide   使用此效果(展现/隐藏)该目标
		 * @param isLean	      是否随机倾斜处理每一块BitmapData
		 * @param isBlur	      是否模糊处理每块BitmapData
		 **/
		private static var eachItemEffect_Gc_Tmp:Array;
		private static var eachItemEffect_Gc_Containers:Array;
		public static function eachItemEffect(target:UIComponent,excuteTime:Number,columnCount:int,rowsCount:int,showOrHide:Boolean,direction:String,isLean:Boolean=false,isBlur:Boolean=false):void
		{
			var parent:Group=target.parent as Group;
			var bitmapData:BitmapData=ImageSnapshot.captureBitmapData(target);
			var bitmapDataBlocks:Array=BitmapDataUtils.bitmapDataBlockCache(bitmapData,columnCount,rowsCount);
			var tmps:Array=[];
			var length:uint=bitmapDataBlocks.length;
			/**装载块显示对象**/
			for(var i:int=0;i<length;i++)
			{
				var img:Group=new Group();
				var bit:Image=new Image();
				bit.source=(bitmapDataBlocks[i] as BitmapDataInfo).bitmapData;
				img.addElement(bit);
				img.width=(bitmapDataBlocks[i] as BitmapDataInfo).bitmapData.width;
				img.height=(bitmapDataBlocks[i] as BitmapDataInfo).bitmapData.height;
				img.x=int(target.x)+(bitmapDataBlocks[i] as BitmapDataInfo).point.x;
				img.y=int(target.y)+(bitmapDataBlocks[i] as BitmapDataInfo).point.y;
				
				parent.addElement(img);
				tmps.push(img);
			}
			/**执行分布**/
			//DistributeUtils.blockDistribute(tmps,excuteTime,DistributeUtils.DOWN_TO_UP,columnCount,rowsCount,target.alpha,onComplete,[eachitemeffect,target,parent]);
			/**预备垃圾回收**/
			eachItemEffect_Gc_Tmp=bitmapDataBlocks;
			eachItemEffect_Gc_Containers=tmps;
		}
		private static function onComplete(functionName:String,target:UIComponent=null,parent:UIComponent=null):void
		{
			setTimeout(gc,200,functionName,target,parent);
		}
		public static function gc(functionName:String,target:UIComponent=null,parent:UIComponent=null):void
		{
			/**执行GC**/
			switch(functionName)
			{
				case awiseffect:
					break;
				case eachitemeffect:
					if(eachItemEffect_Gc_Tmp.length!=0)
					{
						target.visible=true;
						for(var i:int=0;i<eachItemEffect_Gc_Tmp.length;i++)
						{
							parent.removeChild(eachItemEffect_Gc_Containers[i]);
							(eachItemEffect_Gc_Tmp[i] as BitmapDataInfo).bitmapData=null;
							(eachItemEffect_Gc_Tmp[i] as BitmapDataInfo).point=null;
							eachItemEffect_Gc_Tmp[i]=null;
							eachItemEffect_Gc_Containers[i]=null
						}
						eachItemEffect_Gc_Tmp=null;
						eachItemEffect_Gc_Containers=null;
					}
					break;
			}
			System.gc();
			
		}
		public static function clearResource(childList:ArrayCollection,container:Container):void
		{
			if(childList.length!=0)
			{
				for(var i:int=0;i<childList.length;i++)
				{
					container.removeChild(childList[i]);
					childList[i]=null;
				}
			}
		}
	}
}